using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace YATC
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CreditScene :GameScene
    {
        private Model title;
        private Matrix gameWorldRotation = Matrix.Identity;
    
        private Vector2 textPosition, finalTextPosition;
        private int startTime, curentTime;
    	private TimeSpan swTime = TimeSpan.FromMilliseconds(1500);
        private Vector3 position;

		//Songs-Ticks-Tocks
    	private AudioLibrary audio;

        public CreditScene(Game game, Model title)
            : base(game)
        {
            this.title = title;
        	audio = (AudioLibrary) Game.Services.GetService(typeof (AudioLibrary));
        }
        public override void Initialize()
        {
            position = new Vector3(0,-2,0);

            textPosition = new Vector2(10, 780);
            finalTextPosition = textPosition;
			swTime = TimeSpan.FromMilliseconds(1500);

            startTime = System.Environment.TickCount;
            base.Initialize();
        }
        private void DrawTitle()
        {
            var transforms = new Matrix[title.Bones.Count];
            title.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in title.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();


                    effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 30), Vector3.Zero, Vector3.Up);
                    effect.Projection = ((Game1)Game).camera.projectionMatrix;
                    effect.World = gameWorldRotation *
                                   transforms[mesh.ParentBone.Index] *
                                   Matrix.CreateTranslation(position) * Matrix.CreateRotationY(MathHelper.ToRadians(180));
                }
                mesh.Draw();
            }
        }

        public void _Update(GameTime gameTime)
        {
            DrawTitle();
            swTime += gameTime.ElapsedGameTime;

            if (swTime > TimeSpan.FromMilliseconds(1500))
            {
                textPosition.Y -= 0.65f;
                if (finalTextPosition.Y > -100)
                    finalTextPosition.Y -= 0.65f;
                if (textPosition.Y < 500)
                    position.Y += 0.021f;
            }

            curentTime = (System.Environment.TickCount - startTime) / 1000;
        }
        public Vector2 Textposition
        {
            get{ return textPosition;}
        }
        public Vector2 FinalTextPosition
        {
            get{ return finalTextPosition;}
        }
        public bool Stop()
        {
            return curentTime > 32;
        }
		public override void Show()
		{
			MediaPlayer.Play(audio.GetGreditSong());
			base.Show();
		}
		public override void Hide()
		{
			MediaPlayer.Stop();
			base.Hide();
		}
    }
}